package com.eric.windows.refs;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.eric.windows.R;

/**
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> Eric
 * <h3>Date</h3> 2017/8/11 10:41
 * <h3>Copyright</h3> Copyright (c)2017 Shenzhen TLIT  Co., Ltd. Inc. All rights reserved.
 */
public class BookListActivity extends Activity implements BookListFragment.Callbacks {
    // 定义一个旗标，用于标识该应用是否支持大屏幕
    private boolean mTwoPane;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 指定加载R.layout.activity_book_list对应的界面布局文件
        // 但实际上该应用会根据屏幕分辨率家在不同的界面布局文件
        /*屏幕适配-->创建values-sw600dp、values-large文件夹，创建refs.xml文件如下
          <resources>
	      <item type=" layout" name="activity_book_list">
	          	@layout/activity_book_twopane
	       </item>
          </resources>
         */
        setContentView(R.layout.activity_book_list);
        // 如果加载的界面布局文件中包含ID为book_detail_container的组件
        if (findViewById(R.id.book_detail_container) != null){
            mTwoPane = true;
            ((BookListFragment) getFragmentManager().findFragmentById(R.id.book_list))
                    .setActivateOnItemClick(true);
        }
    }
    @Override
    public void onItemSelected(Integer id) {
        if (mTwoPane){
            // 创建Bundle，准备向Fragment传入参数
            Bundle arguments = new Bundle();
            arguments.putInt(BookDetailFragment.ITEM_ID, id);
            // 创建BookDetailFragment对象
            BookDetailFragment fragment = new BookDetailFragment();
            // 向Fragment传入参数
            fragment.setArguments(arguments);
            // 使用fragment替换book_detail_container容器当前显示的Fragment
            getFragmentManager().beginTransaction()
                    .replace(R.id.book_detail_container, fragment).commit();
        }else {
            // 创建启动BookDetailActivity的Intent
            Intent detailIntent = new Intent(this, BookDetailActivity.class);
            // 设置传给BookDetailActivity的参数
            detailIntent.putExtra(BookDetailFragment.ITEM_ID, id);
            // 启动Activity
            startActivity(detailIntent);
        }
    }
}
